<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/12/10 0010
 * Time: 15:32
 */

namespace app\index\model;


class Information extends Base
{
    protected $autoWriteTimestamp = true;
    //关键字获取器
    protected function getKeywordIdAttr($value){
        $informationKey = new InformationKeyword();//分站类
        $key_id =  StrToArray($value);
        $informationKeyDeatail= [];
        foreach ($key_id as $key=>$val){
            $informationKeyDeatail[$key]['keyword_name'] = $informationKey->getFieldValue("keyword_id", $val, "keyword_name");
        }
        return $informationKeyDeatail;
    }

    //分割关键字
    protected function getKeywordsAttr($value){
        $value = str_replace("，",",",$value);
        $keywords = explode(",", $value);//分割成数组
        return $keywords;
    }
    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:资讯列表
     * @param string classification_id
     */
    public function newsList($classification_id = "",$after_limit = "10",$keyword = ""){
        return $this->where(function ($q)use ($classification_id){
            if(!empty($classification_id)){
                //查找该栏目资讯
                $q->where("classification_id",$classification_id);
                }
            })
            ->where(function ($q)use ($keyword){
                if(!empty($keyword)){
                    //模糊查找支持标题和编号
                    $q->whereOr("information_title",'like','%'.$keyword.'%')->whereOr("text_content",'like','%'.$keyword.'%');
                }
            })
            ->where('is_display',1)
            ->where('delete',0)
            ->orderRaw("update_time DESC")
            ->paginate($after_limit,false,[
                'type'     => 'page\marketPage',
                'var_page'  => 'p',
                'query' =>  request()->param(),
            ]);
    }

    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:资讯列表
     * @param string classification_id
     */
    public function informationList($classification_id = "",$limit = "24",$left="8",$center="16"){
        $select = $this->field("information_id,information_title,thumbnail")->where(function ($q)use ($classification_id){
            if(!empty($classification_id)){
                //查找该栏目资讯
                $q->where("classification_id",$classification_id);
            }
        })
        ->where('is_display',1)->where("delete",0)->orderRaw("reading_number desc,update_time DESC")->limit($limit)->select();
        $result =[];
        foreach ($select as $key=>$val){
            if($key<$left){
                $result["left"][$key] = $val;
            }elseif ($key<$center){
                $result["center"][$key] = $val;
            }else{
                $result["right"][$key] = $val;
            }
        }
        return $result;
    }

    /**
     * Author:yang you
     * Time:2018-12-2 10:27:51
     * Describe:资讯列表
     * @param string classification_id
     */
    public function todayOrHotNews($type = "1",$limit = "7"){
        if($type==1){
            $type_name = "update_time";//今日推荐
        }else{
            $type_name = "reading_number";//热门推荐
        }
        return $this
            ->where('is_display',1)
            ->where('delete',0)
            ->orderRaw("$type_name DESC")
            ->limit($limit)
            ->select();
    }

    /**
     * Author:yang you
     * Time:2018-12-21 12:27:51
     * Describe:随机获取热门标签
     * @return int|string
     */
    public function randKeywordInfo(){
        return $this->alias("a")->field("a.keywords,a.information_id")
            ->where("a.keywords is not null")
            ->orderRaw('rand()')
            ->limit(6)
            ->select();
    }

    /**
     * Author:yang you
     * Time:2018-12-21 12:27:51
     * Describe:匹配相关标签
     * @return int|string
     */
    public function relateKeyword($keyword_name=''){
        return $this->alias("a")->field("a.keywords,a.information_id")
            ->where(function ($q)use ($keyword_name){
                if(!empty($keyword_name)){
                    //模糊查找支持标题和编号
                    $q->where("keywords",'like','%'.$keyword_name.'%');
                }
            })
            ->where("a.keywords is not null")
            ->orderRaw('information_id DESC')
            ->select();
    }


}